package com.cksys.achievement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cksys.achievement.entity.Result;
import com.cksys.achievement.entity.ResultUser;
import com.cksys.achievement.enums.ResultStatus;
import com.cksys.achievement.mapper.ResultMapper;
import com.cksys.achievement.service.IResultPictureService;
import com.cksys.achievement.service.IResultService;
import com.cksys.achievement.service.IResultUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tadechen
 * @since 2019-09-26
 */
@Service
public class ResultServiceImpl extends ServiceImpl<ResultMapper, Result> implements IResultService {

    @Autowired
    IResultPictureService resultPictureService;

    @Autowired
    IResultUserService resultUserService;

    @Autowired
    IResultService resultService;

    @Autowired
    ResultMapper resultMapper;

    @Override
    public List<Result> searchResultList(String time, Integer typeId, List<Integer> userIds, Integer uId, ResultStatus resultStatus) {
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        if (typeId != null && typeId != 0) {
            queryWrapper.eq("type_id", typeId);
        }
        if (time != null && !"".equals(time)) {
            String[] times = time.split(" - ");
            queryWrapper.between("get_time", times[0], times[1]);
        }
        if (uId != null && uId != 0) {
            queryWrapper.eq("u_id", uId);
        }
        if (resultStatus != null) {
            queryWrapper.eq("result_status", resultStatus.getResultStatus());
        }
        queryWrapper.orderByDesc("get_time", "insert_time");
        if (userIds != null && userIds.size() > 0) {

            List<Object> rIdList = resultUserService.listObjs(new QueryWrapper<ResultUser>().in("u_id", userIds).groupBy("r_id").select("r_id"));

            if (rIdList.size() == 0) {
                return new ArrayList<Result>();
            }

            queryWrapper.in("id", rIdList);

            return resultService.list(queryWrapper);
        }
        return resultService.list(queryWrapper);
    }

    @Override
    public IPage<Result> searchResultPage(IPage<Result> page, String time, Integer typeId, List<Integer> userIds, Integer uId, ResultStatus resultStatus) {
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        if (typeId != null && typeId != 0) {
            queryWrapper.eq("type_id", typeId);
        }
        if (time != null && !"".equals(time)) {
            String[] times = time.split(" - ");
            queryWrapper.between("get_time", times[0], times[1]);
        }
        if (uId != null && uId != 0) {
            queryWrapper.eq("u_id", uId);
        }
        if (resultStatus != null) {
            queryWrapper.eq("result_status", resultStatus.getResultStatus());
        }
        queryWrapper.orderByDesc("get_time", "insert_time");
        IPage<Result> results = resultService.page(page, queryWrapper);
        if (userIds != null && userIds.size() > 0) {

            List<Object> rIdList = resultUserService.listObjs(new QueryWrapper<ResultUser>()
                    .in("u_id", userIds).groupBy("r_id").select("r_id"));

            if (rIdList.size() == 0) {
                results = new Page<>(0 ,0);
            } else {
                queryWrapper.in("id", rIdList);
                results = resultService.page(page, queryWrapper);
            }
        }
        return results;
    }

    @Override
    public List<Collection<String>> getCalendar() {
        List<LinkedHashMap<String, String>> list = resultMapper.getCalendar();
        List<Collection<String>> result = list.stream().map(LinkedHashMap::values).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<Collection<String>> getByType() {
        List<LinkedHashMap<String, String>> list = resultMapper.getByType();
        LinkedHashMap<String, String> byNullType = resultMapper.getByNullType();
        list.add(0, byNullType);
        List<Collection<String>> result = list.stream().map(LinkedHashMap::values).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<Collection<String>> getByUser() {
        List<LinkedHashMap<String, String>> list = resultMapper.getByUser();
        List<Collection<String>> result = list.stream().map(LinkedHashMap::values).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<Collection<String>> getByTypeWithYear() {
        List<LinkedHashMap<String, String>> list = resultMapper.getByTypeWithYear();
        if (list.size() == 0) {
            return null;
        }
        Set<String> strings = list.get(0).keySet();
        List<Collection<String>> result = list.stream().map(LinkedHashMap::values).collect(Collectors.toList());
        result.add(0 ,strings);
        return result;
    }

    @Override
    public List<String> getTopUser(Integer num) {
        List<HashMap<Integer, String>> topUser = resultMapper.getTopUser(num);
        List<String> username = topUser.stream().map(t -> t.get("real_name")).collect(Collectors.toList());
        return username;
    }

}
